﻿// this file is part of notepad++
// Copyright (C)2003 Don HO <donho@altern.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include "PluginDefinition.h"
#include "menuCmdID.h"
// #include <string>
// #include "curl\curl.h"
#include "../app.h"
// using namespace std;

// namespace app {
// 	string data;
// }
// size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
//
// The plugin data that Notepad++ needs
//
FuncItem funcItem[nbFunc];

//
// The data of Notepad++ that you can use in your plugin commands
//
NppData nppData;

//
// Initialize your plugin data here
// It will be called while plugin loading
void pluginInit(HANDLE /*hModule*/)
{
}

//
// Here you can do the clean up, save the parameters (if any) for the next session
//
void pluginCleanUp()
{
}

//
// Initialization of your plugin commands
// You should fill your plugins commands here
void commandMenuInit()
{

	//--------------------------------------------//
	//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
	//--------------------------------------------//
	// with function :
	// setCommand(int index,                      // zero based number to indicate the order of command
	//            TCHAR *commandName,             // the command name that you want to see in plugin menu
	//            PFUNCPLUGINCMD functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
	//            ShortcutKey *shortcut,          // optional. Define a shortcut to trigger this command
	//            bool check0nInit                // optional. Make this menu item be checked visually
	//            );
	// setCommand(0, TEXT("request args pretty"), request_args_pretty, NULL, false);
	// setCommand(1, TEXT("request args compress"), request_args_compress, NULL, false);
	// setCommand(2, TEXT("get request"), get_request, NULL, false);
	// setCommand(3, TEXT("about"), about, NULL, false);
	setCommand(0, TEXT("about"), about, NULL, false);
	setCommand(1, TEXT("文档长度"), printLength, NULL, false);
	setCommand(2, TEXT("文档内容"), printContent, NULL, false);
	setCommand(3, TEXT("为每一行加上单引号"), addSingleQuotes, NULL, false);
	setCommand(4, TEXT("为每一行加上双引号"), addQuotes, NULL, false);
	setCommand(5, TEXT("为每一行添加逗号结束符"), addCommaEnd, NULL, false);
	setCommand(6, TEXT("压缩成单行"), toSingleLine, NULL, false);
	setCommand(7, TEXT("转成SQL语句"), toSql, NULL, false);
}

//
// Here you can do the clean up (especially for the shortcut)
//
void commandMenuCleanUp()
{
	// Don't forget to deallocate your shortcut here
}

//
// This function help you to initialize your plugin commands
//
bool setCommand(size_t index, PCTSTR cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
	if (index >= nbFunc)
		return false;

	if (!pFunc)
		return false;

	lstrcpy(funcItem[index]._itemName, cmdName);
	funcItem[index]._pFunc = pFunc;
	funcItem[index]._init2Check = check0nInit;
	funcItem[index]._pShKey = sk;

	return true;
}

// void hello(PSTR content)
// {
// 	// Open a new document
// 	::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);

// 	// Get the current scintilla
// 	int which = -1;
// 	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
// 	if (which == -1)
// 		return;
// 	HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

// 	// Say hello now :
// 	// Scintilla control has no Unicode mode, so we use (char *) here
// 	::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)content);
// }

//----------------------------------------------//
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
//----------------------------------------------//
// void request_args_pretty()
// {
// 	// Get the current scintilla
// 	int which = -1;
// 	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
// 	if (which == -1)
// 		return;
// 	HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

// 	// Get content length
// 	int content_length = 0;
// 	content_length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
// 	if (content_length == 0)
// 	{
// 		::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"2");
// 		return;
// 	}
// 	// Get content
// 	PSTR content = (PSTR)malloc((content_length + 1) * sizeof(CHAR));
// 	::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(content_length + 1), (LPARAM)content);

// 	string result;
// 	for (int i = 0; i < content_length; i++)
// 	{
// 		if ('&' == *(content + i) || '?' == *(content + i))
// 		{
// 			result += '\n';
// 		}
// 		else
// 		{
// 			result += *(content + i);
// 		}
// 	}

// 	// Say hello now :
// 	// Scintilla control has no Unicode mode, so we use (char *) here
// 	::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)result.data());

// 	// horizontal scroll
// 	::SendMessage(curScintilla, SCI_SETXOFFSET, 0, 0);

// 	// Free memory
// 	free(content);
// }

// bool start_with(PCSTR a, PCSTR b) {
// 	CHAR x = *a;
// 	CHAR y = *b;

// 	int i = 0;
// 	while (true) {
// 		x = *(a + i);
// 		y = *(b + i);
// 		if (y == 0) {
// 			return true;
// 		}
// 		if (x == 0) {
// 			return false;
// 		}
// 		if (x != y) {
// 			return false;
// 		}
// 		i++;
// 	}
// 	return true;
// }

// void request_args_compress() {
// 	// Get the current scintilla
// 	int which = -1;
// 	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
// 	if (which == -1)
// 		return;
// 	HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

// 	// Get content length
// 	int content_length = 0;
// 	content_length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
// 	if (content_length == 0)
// 	{
// 		::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"2");
// 		return;
// 	}
// 	// Get content
// 	PSTR content = (PSTR)malloc((content_length + 1) * sizeof(CHAR));
// 	::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(content_length + 1), (LPARAM)content);

// 	bool first = false;
// 	if (start_with(content, "http")) {
// 		first = true;
// 	}

// 	string result;
// 	for (int i = 0; i < content_length; i++) {
// 		CHAR c = *(content + i);
// 		if (c == '\n') {
// 			if (first) {
// 				result += "?";
// 				first = false;
// 			}
// 			else {
// 				result += '&';
// 			}

// 		}
// 		else {
// 			result += c;
// 		}
// 	}

// 	// Say hello now :
// 	// Scintilla control has no Unicode mode, so we use (char *) here
// 	::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)result.data());

// 	// horizontal scroll
// 	::SendMessage(curScintilla, SCI_SETXOFFSET, 0, 0);

// 	// Free memory
// 	free(content);
// }

// void get_request() {
// 	int which = -1;
// 	::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
// 	if (which == -1)
// 		return;
// 	HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

// 	// Get content length
// 	int content_length = 0;
// 	content_length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
// 	if (content_length == 0)
// 	{
// 		::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)"2");
// 		return;
// 	}
// 	// Get content
// 	PSTR content = (PSTR)malloc((content_length + 1) * sizeof(CHAR));
// 	::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(content_length + 1), (LPARAM)content);

// 	CURL *curl = curl_easy_init();
// 	// request url
// 	curl_easy_setopt(curl, CURLOPT_URL, content); // url
// 																		// response handler
// 	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

// 	CURLcode res = curl_easy_perform(curl);
// 	if (res == CURLE_OK) {
// 		::SendMessage(curScintilla, SCI_APPENDTEXT, app::data.size(), (LPARAM)app::data.data());
// 	}
// 	else {
// 		::SendMessage(curScintilla, SCI_APPENDTEXT, 0, (LPARAM)"request failed!");
// 	}
// 	curl_easy_cleanup(curl);

// 	free(content);
// }

// void about()
// {
// 	::MessageBox(NULL, TEXT("hello你好"), TEXT("hello"), MB_OK);
// }

// size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
// 	app::data.append((const char *)buffer, nmemb);
// 	return nmemb;
// }
